package com.ruoyi.tranShort.utils;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.mqtt.api.MqttClientApi;
import com.ruoyi.tranShort.mqtt.common.constants.MqttRedisKey;
import com.ruoyi.tranShort.service.impl.ShortMqttConfigServiceImpl;

import java.util.Map;

/**
 * @author wwy
 * @date 2021-08-10 15:22
 */
public class MqttClientUtil {


    public static MqttClientApi getMqttClientApi(){
        return ShortMqttConfigServiceImpl.mqttClientApi;
    }

    /**
     * 根据mac获取信号强度最强的网关
     * @param mac
     * @return
     */
    public static String getTopicMac(String mac){
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        //最大阀值
        int minRessi = 100;
        //阀值最大对应的网关mac地址
        String maxClientId = "";
        String key = MqttRedisKey.LOCATIONTRACKING+mac;
        Map<String, Object> locationMap = redisCache.getCacheMap(key);
        //遍历出阀值，取最大阀值对应的网关mac地址
        for (Map.Entry<String, Object> objectObjectEntry : locationMap.entrySet()) {
            //网关mac地址
            String clientId = String.valueOf(objectObjectEntry.getKey());
            //rssi,阀值
            String rssi = String.valueOf(objectObjectEntry.getValue());
            int rssisInt = Integer.parseInt(rssi,16);
            if(rssisInt<=minRessi){
                minRessi = rssisInt;
                maxClientId = clientId.toLowerCase();
            }
        }
        return maxClientId;
    }
}
